import _ from 'lodash';
import {
  getWarningChart,
  getMicorElements,
  getWaterQualityData,
  geAlltWaterQualityData,
} from '@/services/warningManage';

const WarningModel = {
  namespace: 'warningManage',
  state: {
    warningData: [],
    WaterQualityData: [],
    visiable: false,
    rowIndex: {},
    micorelements: [],
    AlltWaterQualityData: [],
  },

  effects: {
    *getData({ payload }, { call, put, select }) {
      const response = yield call(getWarningChart);
      const warningChart = _.get(response, 'data.alarminfo', []);
      yield put({
        type: 'updateWarningData',
        payload: {
          warningChart,
        },
      });
    },
    *getMicorElements({ payload }, { call, put, select }) {
      const response = yield call(getMicorElements, payload);
      const micorelements = _.get(response, 'data', []);
      yield put({
        type: 'updateMicorElements',
        payload: {
          micorelements,
        },
      });
    },
    *getWaterQualityData({ payload }, { call, put, select }) {
      const response = yield call(getWaterQualityData);
      const WaterQualityData = _.get(response, 'data', []);
      console.log(WaterQualityData);
      yield put({
        type: 'updateWaterQualityData',
        payload: { WaterQualityData },
      });
    },
    *geAlltWaterQualityData({ payload }, { call, put, select }) {
      const response = yield call(geAlltWaterQualityData, payload);
      const AlltWaterQualityData = _.get(response, 'data', []);
      yield put({
        type: 'updateAlltWaterQualityData',
        payload: {
          AlltWaterQualityData,
        },
      });
    },
  },

  reducers: {
    changeVisible(state) {
      return {
        ...state,
        visiable: !state.visiable,
      };
    },

    displayRowIndex(state, { payload }) {
      return {
        ...state,
        rowIndex: payload.rowIndex,
      };
    },

    updateWarningData(state, { payload }) {
      return {
        ...state,
        warningData: payload.warningChart,
      };
    },
    updateMicorElements(state, { payload }) {
      return {
        ...state,
        micorelements: payload.micorelements,
      };
    },
    updateWaterQualityData(state, { payload }) {
      console.log(payload.WaterQualityData);
      return {
        ...state,
        WaterQualityData: payload.WaterQualityData,
      };
    },
    updateAlltWaterQualityData(state, { payload }) {
      return {
        ...state,
        AlltWaterQualityData: payload.AlltWaterQualityData,
      };
    },
  },
};

export default WarningModel;
